package com.myutils;

import java.io.File;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;


public class XmlConvertToText {

	public static void main(String[] args) {
		String dirPath = "D:\\temp";
		File[] files = FileUtil.getFilesOfDir(dirPath, "([^\\s]+(\\.(?i)xml)$)");

		for(File xmlFile : files){
			Document document = XmlUtil.getXmlDocument(xmlFile);
			
			StringBuilder strbuilder = new StringBuilder();
			String linespr = "\r\n";//换行符
			
			Element rootElm = document.getRootElement();
			String type = rootElm.attributeValue("type");//账单类型:cah,fap...
			for(Iterator<?> iter = rootElm.elementIterator("einvoice"); iter.hasNext();){
				Element einvoiceElm = (Element)iter.next();//电子发票：一封账单数据
				
				Element invoiceElm = einvoiceElm.element("invoice");
				String barcode = invoiceElm.elementText("barcode");//1-2 条形码ID
				String postcode = invoiceElm.elementText("postcode");//1-3 邮编
				String address = invoiceElm.elementText("address");//1-4 地址
				String name = invoiceElm.elementText("name");//1-5 客户名称
				String servicecodelist = invoiceElm.elementText("servicecodelist");//1-6 用户证号
				String acctno = invoiceElm.elementText("acctno");//1-7 账户号
				String bankcode = invoiceElm.elementText("bankcode");//1-8 转帐银行
				
				Element invoicecycleElm = invoiceElm.element("invoicecycle");
				String startdate = "";
				String enddate = "";
				String validdate= "";
				for(Iterator<?> proIter = invoicecycleElm.elementIterator("property"); proIter.hasNext();){
					Element propertyElm = (Element)proIter.next();
					String datename = propertyElm.attributeValue("name");
					String datevalue = propertyElm.attributeValue("value");
					if(StringUtils.equals(datename, "startdate")){
						startdate = datevalue;//1-9 计费周期起始日期 yyyyMMdd
					}else if(StringUtils.equals(datename, "enddate")){
						enddate = datevalue;//1-10 计费周期截止日期 yyyyMMdd
					}else if(StringUtils.equals(datename, "validdate")){
						validdate = datevalue;//1-11 最后缴费日期/转帐日期 yyyyMMdd
					}
				}
				String invoicecycle = invoicecycleElm.attributeValue("name");//1-12 账期yyyyMM
				String deductfail = "";//...?? 1-13 扣款不成功原因
				String amount = invoiceElm.elementText("amount");//1-14 总金额
				String transactionid = invoiceElm.attributeValue("transactionid");//1-15 交易号码 定长（不足6位时左补0）
				String area = invoiceElm.elementText("area");//1-16 区域代码
				
				Element remarkElm = invoiceElm.element("remark");
				String remarkcode = remarkElm.attributeValue("value");//1-17 备注代码
				String remarkdesc = "";//1-18 备注描述
				for(Iterator<?> paramIter = remarkElm.elementIterator("param"); paramIter.hasNext();){
					Element paramElm = (Element)paramIter.next();
					remarkdesc += paramElm.attributeValue("value") + "#";
				}
				
				String customerid = einvoiceElm.elementText("customerid");//1-19 客户证号
				String stbmodel = "5505";//...?? 1-20 机顶盒型号
				String macAddress = einvoiceElm.element("eaddress").elementText("terminaldevice");//1-21 mac地址
				
				strbuilder.append(linespr + "\"1\"|\"" + barcode + "\"|");
				strbuilder.append("\"" + postcode + "\"|");
				strbuilder.append("\"" + address + "\"|");
				strbuilder.append("\"" + name + "\"|");
				strbuilder.append("\"" + servicecodelist + "\"|");
				strbuilder.append("\"" + acctno + "\"|");
				strbuilder.append("\"" + bankcode + "\"|");
				strbuilder.append("\"" + startdate + "\"|");
				strbuilder.append("\"" + enddate + "\"|");
				strbuilder.append("\"" + validdate + "\"|");
				strbuilder.append("\"" + invoicecycle + "\"|");
				strbuilder.append("\"" + deductfail + "\"|");
				strbuilder.append("\"" + amount + "\"|");
				strbuilder.append("\"" + transactionid + "\"|");
				strbuilder.append("\"" + area + "\"|");
				strbuilder.append("\"" + remarkcode + "\"|");
				strbuilder.append("\"" + remarkdesc + "\"|");
				strbuilder.append("\"" + customerid + "\"|");
				strbuilder.append("\"" + stbmodel + "\"|");
				strbuilder.append("\"" + macAddress + "\"" + linespr);//第一行客户基本信息结束，换行
				
				for(Iterator<?> subIter = invoiceElm.elementIterator("subinvoice"); subIter.hasNext();){
					Element subinvoiceElm = (Element) subIter.next();
					String servicecode = subinvoiceElm.attributeValue("servicecode");
					strbuilder.append("\"2\"|\"" + servicecode + "\"" + linespr);//第二行，换行
					
					for(Iterator<?> payIter = subinvoiceElm.elementIterator("payment");payIter.hasNext();){
						Element paymentElm = (Element)payIter.next();
						strbuilder.append("\"3\"|\"" + paymentElm.attributeValue("title") + "\"|\"" + paymentElm.attributeValue("amount") + "\"" + linespr);//一级费用，换行
						
						for(Iterator<?> itemIter = paymentElm.elementIterator("item"); itemIter.hasNext();){
							Element itemElm = (Element)itemIter.next();
							strbuilder.append("\"4\"|\"" + itemElm.attributeValue("title") + "\"|\"" + itemElm.attributeValue("amount") + "\"" + linespr);//二级费用，换行
						}
					}
				}
				
				strbuilder.append("\"5\"|\"1\"");//结束行
			}
			String txtpath = dirPath + "\\" + type + "_" + DateUtil.getNowTime("yyyyMMdd") + "_01.txt";
			
			System.out.println(txtpath);
			String content = StringUtils.replaceOnce(strbuilder.toString(), linespr, "");
			System.out.println(content);
			
			FileUtil.writeFile(new File(txtpath), content);
			
		}
		
		
	}
}
